package com.wc.alorithm.string.trietree;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author congege
 * @Description Tire 树
 * @Date Created in 2023/12/2-10:25
 */
public class Trie {
    // 是否为关键节点
    private boolean isEnd;
    // 节点的孩子
    private final Map<Character, Trie> son;
    private int num;

    public Trie() {
        isEnd = false;
        son = new HashMap<>();
        num = 0;
    }

    public void insert(String word) {
        Trie node = this;
        for (char c : word.toCharArray()) {
            if (!node.son.containsKey(c)) {
                Trie son = new Trie();
                node.son.put(c, son);
            }
            node = node.son.get(c);
            node.num++;
        }
        node.isEnd = true;
    }

    public boolean query(String word){
        Trie node = this;
        for(char c : word.toCharArray()){
            if (node.son.containsKey(c)) {
                node = node.son.get(c);
            }else {
                return false;
            }
        }

        return node.isEnd;
    }
}
